let requestUrl = null;

function addHandler() {
	requestUrl = "/xingyin_category/insert";

	$("#xingyin_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#xingyin_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/xingyin_category/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#type").combobox("setValue", rowData.type);
		$("#grade").numberspinner("setValue", rowData.grade);

		$("#xingyin_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function distributeHandler() {
	let list = $("#xingyin_datalist");

	list.datalist("reload");
	list.datalist("selectRecord", 1);

	$("#xingyin_texiao").dialog("open");
}

function deleteHandler(categoryId) {
	$.messager.confirm("提示", "是否确认删除？", function(bool) {
		if (bool) {
			ajaxPost("/xingyin_category/deleteById", {
				id: categoryId
			}, function(resp) {
				showMsg(resp.message);

				$("#xingyin_list").datagrid("reload");
			}, error);
		}
	});
}

function imageHandler() {
	let rowData = $("#xingyin_list").datagrid("getSelected");

	if (rowData) {
		requestUrl = "/xingyin_category/updateById";

		$("#xingyin_id").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录。", "warning");
	}
}

$(document).ready(function() {

	$("#name").textbox({
		width: 150,
		required: true
	});

	$("#type").combobox({
		url: "/xingyin_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		groupField: "parentId",
		groupFormatter: function(group) {
			ajaxGet("/xingyin_type/selectById", {
				id: group
			}, function (resp) {
				let data = resp.data;

				$("#type_group_" + group).html(data.name);
			}, error);

			return "<div class='blue' id='type_group_" + group + "'></div>";
		}
	});

	$("#grade").numberspinner({
		min: 65,
		max: 105,
		width: 150,
		increment: 20,
		required: true
	});

	$("#xingyin_dialog").dialog({
		title: "星印信息",
		width: 500,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#xingyin_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#xingyin_dialog").dialog("close");
						$("#xingyin_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#xingyin_dialog").dialog("close");
				$("#xingyin_form").form("clear");
			}
		}]
	});

	/******************************************************************************************************/

	$("#xingyin_type").combobox({
		url: "/xingyin_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: false,
		panelHeight: "auto",
		onSelect: function (record) {
			if (record) {
				$("#xingyin_datalist").datalist("reload", {
					type: record.id
				});
			}
		}
	});

	$("#xingyin_datalist").datalist({
		url: "/xingyin_category/selectByType",
		idField: "id",
		valueField: "id",
		textField: "name",
		width: 180,
		height: 380,
		textFormatter: function (value, rowData, rowIndex) {
			let content = ""

			content += 	"<table>";
			content += 	"<tr>";
			content += 		"<td rowspan='2'>";
			content += 			"<img height='56' src='" + (rowData.image ? rowData.image : "/images/blank.png") + "' />";
			content += 		"</td>";

			content += 		"<td>";
			content += 			"<span>" + rowData.name + "</span>";
			content += 		"</td>";
			content += 	"</tr>";

			content += 	"<tr>";
			content += 		"<td>";
			content += 			"<div id='xingyin_type_" + rowIndex + "'></div>";
			content += 		"</td>";
			content += 	"</tr>";
			content += "</table>";

			ajaxGet("/xingyin_type/selectById", {
				id: rowData.type
			}, function (resp) {
				$("#xingyin_type_" + rowIndex).html(resp.data.name)
			}, error);

			return content;
		},
		onSelect: function(rowIndex, rowData) {
			if (rowData) {
				$("#texiao_list").treegrid("reload", {
					xingyinId: rowData.type
				});
			}
		}
	});

	$("#texiao_list").treegrid({
		url: "/xingyin_texiao/listTree",
		method: "get",
		idField: "id",
		treeField: "name",
		width: 635,
		height: 410,
		fitColumns: true,
		columns:[[
			{title: "特效名称", field: "name", align: 'left', width: 150},
			{title: "说明", field: "note", align: 'left', width: 400,
				formatter: function(value) {
					if (value) {
						return "<div class='ell' title='" + value + "'>" + value + "</div>";
					}
				}
			}
		]],
		onContextMenu: function (e) {
			e.preventDefault();

			// 显示右键菜单
			$("#mm").menu("show", {
				left: e.pageX,
				top: e.pageY
			});
		}
	});

	$("#xingyin_texiao").dialog({
		title: "星印特效"
	});

	/******************************************************************************************************/

	$("#mm-insert").on("click", function() {
		let rowData = $("#xingyin_datalist").datalist("getSelected");

		if (rowData) {
			ajaxGet("/xingyin_texiao/selectTexiaoIds", {
				xingyinId: rowData.type
			}, function (resp) {
				let data = resp.data;
				let list = $("#texiaoList");

				list.datalist("reload");
				list.datalist("unselectAll");

				if (data && data.length > 0) {
					for (let i = 0; i < data.length; i++) {
						list.datalist("selectRecord", data[i]);
					}
				}

				$("#texiao_dialog").dialog("open");
			}, error);
		}
	});

	$("#mm-update").on("click", function() {
		unopen();
	});

	$("#mm-delete").on("click", function() {
		let xingyin = $("#xingyin_datalist").datalist("getSelected");
		let texiao = $("#texiao_list").treegrid("getSelected");

		if (!texiao.children) {
			$.messager.confirm("提示", "是否确认删除？", function(bool) {
				if (bool) {
					ajaxPost("/xingyin_texiao/delete", {
						xingyinId: xingyin.type,
						texiaoId: texiao.id
					}, function(resp) {
						showMsg(resp.message);

						$("#texiao_list").treegrid("reload");
					}, error);
				}
			});
		} else {
			unopen();
		}
	});

	/******************************************************************************************************/

	$("#texiaoList").datalist({
		url: "/xingyin_texiao_category/selectAll",
		idField: "id",
		valueField: "id",
		textField: "name",
		width: 800,
		height: 400,
		lines: true,
		checkbox: true,
		singleSelect: false,
		checkOnSelect: true,
		selectOnCheck: true,
		textFormatter: function (value, rowData, rowIndex) {
			let content = "";

			content += 	"<table>";
			content += 		"<tr>";
			content += 			"<td rowspan='2'>";
			content += 				"<img height='40' src='" + (rowData.image ? rowData.image: "/images/blank.png")  + "' />";
			content += 			"</td>";

			content += 			"<td>";
			content += 				"<span>" + rowData.name + "</span>";
			content += 			"</td>";
			content += 		"</tr>";

			content += 		"<tr>";
			content += 			"<td>";
			content += 				"<div>" + rowData.note + "</div>";
			content += 			"</td>";
			content += 		"</tr>";
			content += "</table>";

			return content;
		}
	});

	$("#texiao_dialog").dialog({
		title: "星印特效",
		buttons: [{
			iconCls: "icon-ok",
			text: "确认",
			handler: function() {
				let rows = $("#texiaoList").datalist("getChecked");

				if (rows && rows.length > 0) {
					let xingyin = $("#xingyin_datalist").datalist("getSelected");

					if (xingyin) {
						let texiaoIds = [];

						for (let i = 0; i < rows.length; i++) {
							let data = rows[i];

							texiaoIds.push(data.id);
						}

						ajaxPost("/xingyin_texiao/distribute", {
							texiaoIds: JSON.stringify(texiaoIds),
							xingyinId: xingyin.type
						}, function (resp) {
							showMsg(resp.message);

							$("#texiao_dialog").dialog("close");
							$("#texiao_list").treegrid("reload");
						}, error);
					} else {
						alertMsg("请在左侧列表选择一条记录！", "warning");
					}
				}
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#xingyin_list", "/xingyin_category/upload");

	fetchUploadDialog("/xingyin_category/updateImageById", function () {
		$("#xingyin_list").datagrid("reload");
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#xingyin_list").datagrid({
		url: "/xingyin_category/selectByPage",
		height: height_table,
		method: "get",
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-shield",
			text: "分配",
			handler: function() {
				distributeHandler();
			}
		}, "-", imageUpdateButton],
		columns: [[
			{field: "id", hidden: true},
			{field: "name", title: "星印名称", align: "center", width: 100},
			{field: "type", title: "星印类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/xingyin_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#type_" + rowIndex).html(data.name);
					}, error);

					return "<div class='blue' id='type_" + rowIndex + "' ></div>";
				}
			},
			{field: "grade", title: "星印等级", align: "center", width: 100},
			{field: "image", title: "图片", align: "center", width: 100,
				formatter: function(value) {
					return "<img alt='暂无图片' height='" + size + "' src='" + value + "' />";
				}
			},
			deleteColumn
		]]
	});

});